package com.tsing.cedap.rest.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsing.cedap.rest.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tsing.cedap.rest.vo.reqVo.UserPageReqVo;
import com.tsing.cedap.rest.vo.respVo.MiniGetUserInfoRespVo;
import com.tsing.cedap.rest.vo.respVo.UserPageRespVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2022-01-21
 */
public interface UserMapper extends BaseMapper<UserEntity> {

    @Select("select u.id,u.name,u.nickname,u.phone,u.avatar,u.number,u.college_id,\n" +
            "c.name collegeName,ca.id campusId,ca.name campusName,s.id schoolId,s.name schoolName\n" +
            "from cedap_user u\n" +
            "left join cedap_college c on u.college_id=c.id\n" +
            "left join cedap_campus ca on c.campus_id=ca.id\n" +
            "left join cedap_school s on ca.school_id=s.id\n" +
            "where u.id=#{id}\n" +
            "limit 1")
    MiniGetUserInfoRespVo miniUserInfo(@Param("id") String id);

    @Select("<script>" +
            "select u.*,concat(c.name,'-',s.name,'-',ca.name,'-',c.name) college\n" +
            "from cedap_user u\n" +
            "left join cedap_college c on u.college_id=c.id\n" +
            "left join cedap_campus ca on c.campus_id=ca.id\n" +
            "left join cedap_school s on ca.school_id=s.id\n" +
            "<where>" +
            "<if test=\"reqVo.phone != null and reqVo.phone != ''\">and u.phone like concat('%',#{reqVo.phone},'%')</if>" +
            "<if test=\"reqVo.name != null and reqVo.name != ''\">and u.name like concat('%',#{reqVo.name},'%')</if>" +
            "</where>" +
            "</script>")
    List<UserPageRespVo> userPage(IPage<UserPageRespVo> page, @Param("reqVo") UserPageReqVo reqVo);

    @Select("select u.*,concat(c.name,'-',s.name,'-',ca.name,'-',c.name) college\n" +
            "from cedap_user u\n" +
            "left join cedap_college c on u.college_id=c.id\n" +
            "left join cedap_campus ca on c.campus_id=ca.id\n" +
            "left join cedap_school s on ca.school_id=s.id\n" +
            "where u.id=#{id}\n" +
            "limit 1")
    UserPageRespVo getUser(@Param("id") String id);
}
